/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package framework.dal;

//Classes necessárias para uso de Banco de dados //
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

//Início da classe de conexão//
public class Link
{

    private static String path;
    private static String dbName;
    private static String username;
    private static String password;
    private static Connection connection = null;
    private static String status = "Não conectou...";

    public static void setParameters(String url, String nameDB, String user, String pass, String nameTableItem)
    {
        path = "jdbc:mysql://" + url;
        dbName = nameDB;
        username = user;
        password = pass;
        ItemDB.setNameTableItem(nameTableItem);
    }

    public static java.sql.Connection getConnection()
    {
        try
        {
            String driverName = "com.mysql.jdbc.Driver";
            Class.forName(driverName);
            connection = DriverManager.getConnection(path, username, password);

            if (connection != null)
            {
                status = "Conectado com sucesso!";
            } else
            {
                status = "Não foi possivel realizar conexão";
            }

            return connection;

        } catch (ClassNotFoundException e)
        {
            System.out.println("O driver expecificado nao foi encontrado.");
            return null;
        } catch (SQLException e)
        {
            System.out.println("Nao foi possivel conectar ao Banco de Dados.");
            return null;
        }
    }

    public static String getStatus()
    {
        return status;
    }

    public static boolean close()
    {
        try
        {
            getConnection().close();
            return true;
        } catch (SQLException e)
        {
            return false;
        }
    }

    public static java.sql.Connection resetConnection()
    {

        Link.close();
        return getConnection();
    }

    public static boolean add(String tableName, String values)
    {
        try
        {
            Statement stm = Link.getConnection().createStatement();
            String str = "INSERT INTO " + dbName + "." + tableName + " VALUES " + values + ";";
            int rs = stm.executeUpdate(str);
            return true;
        } catch (SQLException e)
        {
            return false;
        }
    }

    public static boolean delete(String tableName, String where)
    {
        try
        {
            Statement stm = Link.getConnection().createStatement();
            String str = "DELETE FROM " + dbName + "." + tableName + " WHERE " + where + ";";
            int rs = stm.executeUpdate(str);

            return true;
        } catch (SQLException e)
        {
            System.out.println("ERRO: Falha ao tentar deletar o " + tableName + ": " + where);
            return false;
        }
    }

    public static boolean executeUpdate(String s)
    {
        try
        {
            Statement stm = Link.getConnection().createStatement();
            int rs = stm.executeUpdate(s);

            return true;
        } catch (SQLException e)
        {
            return false;
        }
    }
    
    public static ResultSet executeQuery(String query)
    {
        try
        {
            Statement stm = Link.getConnection().createStatement();
            return stm.executeQuery(query);
        } catch (SQLException e)
        {
            return null;
        }
    }
    
    public static boolean update(String tableName, String set, String where)
    {
        try
        {
            Statement stm = Link.getConnection().createStatement();
            String s = "UPDATE " + dbName + "." + tableName + " SET " + set + " WHERE " + where + ";";
            int rs = stm.executeUpdate(s);

            return true;
        } catch (SQLException e)
        {
            return false;
        }
    }

    public static ResultSet select(String tableName, String select, String where)
    {
        try
        {
            Statement stm = Link.getConnection().createStatement();
            String s = "SELECT " + select + " FROM " + dbName + "." + tableName + " WHERE " + where;
            return stm.executeQuery(s);
        } catch (SQLException e)
        {
            return null;
        }
    }
}